<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Log;

class ConfigController extends Controller
{
    protected function test()
    {
        $q['company'] = 'tcl';
        $q['device_type'] = 'RT95';
        $q['mac'] = '112233445566';
        $q['wifi_mac'] = '665544332211';
        $q['uuid'] = 'asdfghjkl';
        $q['sdkversion'] = '1';
        $q['appid'] = 'xinbang';
        $appkey = '7947e0ae';
        $q['appname'] = 'com.rockitv.android';
        $q['appversion'] = 'v1.1.1';
        $q['timestamp'] = strval(time());
        $q['security'] = md5($q['uuid'].$q['timestamp'].$appkey);

        return "<a href='/config/ad?".http_build_query($q)."'>Go</a>";

    }

    protected function ad(Request $request)
    {
        $company = $request->input('company', '');
        $device_type = $request->input('device_type', '');
        $mac = $request->input('mac', '');
        $wifi_mac = $request->input('wifi_mac', '');
        $uuid = $request->input('uuid', '');
        $sdkversion = $request->input('sdkversion', '');
        $appid = $request->input('appid', '');
        $appname = $request->input('appname', '');
        $appversion = $request->input('appversion', '');
        $timestamp = $request->input('timestamp', '0');
        $security = $request->input('security', '');
        $now = time();
        $time_limit = 600;

        $apps = [
            "yidian" => [
                "app_key" => 'ae0205a9',
                "dataset" => [
                    [
                        'point_id' => 'yidian-kaiji-shipin',
                        'type' => 1,
                        'show_rate' => 3,
                        'delay' => '1-10',
                    ],
                    [
                        'point_id' => 'yidian-kaiji-tupian',
                        'type' => 1,
                        'show_rate' => 3,
                        'delay' => '30-180',
                    ],
                    [
                        'point_id' => 'yidian-tiepian-shipin',
                        'type' => 2,
                        'show_rate' => 5,
                        'delay' => '360-180',
                        'interval' => '300-600'
                    ],
                    [
                        'point_id' => 'yidian-kaiping-shipin',
                        'type' => 3,
                        'show_rate' => 3,
                        'delay' => '360-180',
                        'interval' => '600-3600'
                    ]
                ]
            ],
            "xinbang" => [
                "app_key" => '7947e0ae',
                "config" => [
                    "company" => "CHxinbang",
                    "device_type" => ""
                ],
                "dataset" => [
                    [
                        'point_id' => 'xinbang-kaiji-shipin',
                        'type' => 1,
                        'show_rate' => 3,
                        'delay' => '1-10',
                    ],
//                    [
//                        'point_id' => 'xinbang-kaiji-tupian',
//                        'type' => 1,
//                        'show_rate' => 3,
//                        'delay' => '30-180',
//                    ]
//                    [
//                        'point_id' => 'xinbang-tiepian-shipin',
//                        'type' => 2,
//                        'show_rate' => 5,
//                        'delay' => '360-180',
//                        'interval' => '300-600'
//                    ],
//                    [
//                        'point_id' => 'xinbang-kaiping-shipin',
//                        'type' => 3,
//                        'show_rate' => 3,
//                        'delay' => '360-180',
//                        'interval' => '600-3600'
//                    ]
                ]
            ]
        ];

        $timestamp = intval($timestamp);
        if($timestamp > 1521536734000) {
            $timestamp = $timestamp / 1000;
        }
        if (abs($now - $timestamp) > $time_limit) {
            Log::error(implode(";", [$company, $device_type, $mac, $wifi_mac, $uuid, $sdkversion, $appname, $appversion, $timestamp]));
            return response()->json([
                "code" => 101,
                "message" => '时间戳错误'
            ]);
        }

        if (!$appid || !array_key_exists($appid, $apps) || md5($uuid.$timestamp.$apps[$appid]["app_key"]) != $security) {
            Log::error(implode(";", [$company, $device_type, $mac, $wifi_mac, $uuid, $sdkversion, $appname, $appversion, $timestamp]));
            return response()->json([
                "code" => 102,
                "message" => 'appid或者appkey错误'
            ]);
        }


        Log::info(implode(";", [$company, $device_type, $mac, $wifi_mac, $uuid, $sdkversion, $appname, $appversion, $timestamp]));

        return response()->json([
            "code" => 0,
            "config" => isset($apps[$appid]['config']) ? $apps[$appid]['config'] : [],
            "dataset" => isset($apps[$appid]['dataset']) ? $apps[$appid]['dataset'] : []
        ]);

    }

    public function powerchart()
    {
        $range = [277233,175078,130273,110221,101868,110615,176248,287372,380385,458880,519729,619334,739104,683393,572522,558081,611220,756810,961360,1151620,1211444,1087611,779425,472158];
        return response()->json([
            "code" => 0,
            "dataset" => $range
        ]);
    }


}
